package com.arraylist;

import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.Random;

public class My_ArrayList {
    int []elem;
    int size;

    public static void main(String[] args) {
        int[]arr=new int [100000];
        for(int i=0;i<arr.length;i++){
            arr[i]=new Random().nextInt(1000000)+1;
        }
        long merge=System.currentTimeMillis();
        A.mergesort(arr,0,arr.length-1);
        long merge2=System.currentTimeMillis();
        System.out.println(merge2-merge);
        //
          for(int i=0;i<arr.length;i++){
              arr[i]=new Random().nextInt(1000000)+1;
          }
          long quick=System.currentTimeMillis();
          A.mergesort(arr,0,arr.length-1);
          long quick2=System.currentTimeMillis();
          System.out.println(quick2-quick);

        for(int i=0;i<arr.length;i++){
            arr[i]=new Random().nextInt(1000000)+1;
        }
        long s=System.currentTimeMillis();
        A.sort(arr);
        long s2=System.currentTimeMillis();
        System.out.println(s2-s);
    }
}

class A{
    public static void sort(int[]arr){
        for(int i=0;i<arr.length-1;i++){
            int min=arr[i];
            int index=i;
            for(int j=i+1;j<arr.length;j++){
                if(arr[j]<min){
                    min=arr[j];
                    index=j;
                }
            }
            if(index!=i){
                arr[index]=arr[i];
                arr[i]=min;
            }
        }
    }
    public static void quicksort(int arr[],int left,int right){
        if(left>=right) {
            return;
        }
        int par=partition(arr,left,right);
        quicksort(arr,left,par-1);
        quicksort(arr,par+1,right);
    }
    public static int partition(int []arr,int start,int end){
        int pivot=arr[start];
        int i=start;
        while(start<end){
            while(start<end&&arr[end]>=pivot){
                end--;
            }
            while(start<end&&arr[start]<=pivot){
                start++;
            }
            swap(arr,start,end);
    }
        swap(arr,start,i);
        return start;
}
public static void swap(int []arr,int i,int j){
    int tem=arr[i];
    arr[i]=arr[j];
    arr[j]=tem;
    }
    public static void mergesort(int[]arr,int left,int right){
        if(left==right){
            return;
        }
        int mid=(left+right)/2;
        mergesort(arr,left,mid);
        mergesort(arr,mid+1,right);
        merge(arr,left,right,mid);
    }
    public static void merge(int[] arr,int left,int right,int mid){
        int s1=left;
        int e1=mid;
        int s2=mid+1;
        int e2=right;
        int k=0;
        int[]temarr=new int[right-left+1];
        while(s1<e1&&s2<e2){
            if(arr[s1]<=arr[s2]){
                temarr[k++]=arr[s1++];
            }
            else{
                temarr[k++]=arr[s2++];
            }
        }
        while(s1<e1){
            temarr[k++]=arr[s1++];
        }
        while(s2<e2) {
            temarr[k++] = arr[s2++];
        }
        for(int i=0;i<temarr.length;i++){
            arr[i+left]=temarr[i];
        }
    }
}
